package com.cms.clash.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.clash.dto.TreeNode;
import com.cms.clash.entity.ArchiveTypeEntity;
import com.cms.clash.mapper.ArctypeMapper;
import com.cms.clash.service.IArcTypeService;
import com.cms.clash.util.TemplateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

/**
 * 文档栏目表-实现类
 * @author cms
 * @since 2024-07-27
 */
@Service
public class ArchiveTypeService extends ServiceImpl<ArctypeMapper, ArchiveTypeEntity> implements IArcTypeService {

    /**
     * 查询所有的分类
     * @return
     */
    public List<ArchiveTypeEntity> getTypeList() {
        LambdaQueryWrapper<ArchiveTypeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArchiveTypeEntity::getStatus, 1);
        List<ArchiveTypeEntity> entityList = this.baseMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(entityList)) {
            for (ArchiveTypeEntity entity : entityList) {
                if (entity.getPath().startsWith("/") || entity.getPath().startsWith("http")){
                    entity.setTypeLink(entity.getPath());
                }else {
                    entity.setTypeLink("/" + entity.getPath());
                }
            }
        }
        return entityList;
    }


    /**
     * 构建分类树
     * @return
     */
    public LinkedList<TreeNode> getTypeNavList() {
        LinkedList<TreeNode> navList = new LinkedList<>();
        List<ArchiveTypeEntity> list = getTypeList();
        if (CollectionUtils.isEmpty(list)) {
            return navList;
        }

        // 构建二级分类
        List<ArchiveTypeEntity> subList = new ArrayList<>();
        for (ArchiveTypeEntity entity : list) {
            if (0 == entity.getParentId().intValue()) {
                TreeNode treeNode = new TreeNode();
                BeanUtils.copyProperties(entity, treeNode);
                treeNode.setHasChildren(false);
                treeNode.setChildren(new LinkedList<>());
                treeNode.setTypeLink(entity.getPath());
                if (!treeNode.getTypeLink().startsWith("http") && !treeNode.getTypeLink().startsWith("/")){
                    treeNode.setTypeLink("/" + entity.getPath());
                }
                navList.add(treeNode);
                continue;
            }
            subList.add(entity);
        }

        // 组装二级目录,目前也只有二级目录
        for (TreeNode parentNode : navList) {
            for (ArchiveTypeEntity subNode : subList) {
                if (parentNode.getId().intValue() == subNode.getParentId().intValue()) {
                    TreeNode treeNode = new TreeNode();
                    BeanUtils.copyProperties(subNode, treeNode);
                    parentNode.getChildren().add(treeNode);
                }
            }
            if (CollectionUtils.isNotEmpty(parentNode.getChildren())) {
                parentNode.setHasChildren(true);
            }
        }
        return navList;
    }

    /**
     * 获取缓存的分类树
     * @return
     */
    public List<TreeNode> getAllNavTree() {
        String key = "typeNavList";
        String json = (String) TemplateUtil.cache.get(key, t -> mappingFunction(key).apply(key));
        return JSON.parseArray(json, TreeNode.class);
    }

    private Function<String, Object> mappingFunction(String key) {
        return t -> JSON.toJSONString(getTypeNavList());
    }

    /**
     * 获取分类映射map
     * @return
     */
    public Map<Integer, ArchiveTypeEntity> getAllNavMap() {
        String key = "typeNavMap";
        Map<Integer, ArchiveTypeEntity> map = (Map<Integer, ArchiveTypeEntity>) TemplateUtil.cache.get(key, t -> mappingMapFunction(key).apply(key));
        return map;
    }

    private Function<String, Object> mappingMapFunction(String key) {
        Map<String, ArchiveTypeEntity> map = new HashMap<>();
        List<ArchiveTypeEntity> typeList = getTypeList();
        for (ArchiveTypeEntity entity : typeList) {
            map.put("k" + entity.getId(), entity);
        }
        return t -> map;
    }

    /**
     * 获取分类映射路径map
     * @return
     */
    public Map<String, Integer> getTypePathMap() {
        String key = "typePathMap";
        Map<String, Integer> map = (Map<String, Integer>) TemplateUtil.cache.get(key, t -> mappingTypePathFunction(key).apply(key));
        return map;
    }

    private Function<String, Object> mappingTypePathFunction(String key) {
        Map<String, Integer> map = new HashMap<>();
        List<ArchiveTypeEntity> typeList = getTypeList();
        for (ArchiveTypeEntity entity : typeList) {
            map.put(entity.getTypeLink(), entity.getId());
        }
        return t -> map;
    }


}
